package com.qingcheng.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qingcheng.dao.RoleMapper;
import com.qingcheng.dao.RoleResourceMapper;
import com.qingcheng.entity.PageResult;
import com.qingcheng.pojo.system.Role;
import com.qingcheng.pojo.system.RoleResource;
import com.qingcheng.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private RoleResourceMapper roleResourceMapper;

    /**
     * 返回全部记录
     *
     * @return
     */
    public List<Role> findAll() {
        return roleMapper.selectAll();
    }

    /**
     * 分页查询
     *
     * @param page 页码
     * @param size 每页记录数
     * @return 分页结果
     */
    public PageResult<Role> findPage(int page, int size) {
        PageHelper.startPage(page, size);
        Page<Role> roles = (Page<Role>) roleMapper.selectAll();
        return new PageResult<Role>(roles.getTotal(), roles.getResult());
    }

    /**
     * 条件查询
     *
     * @param searchMap 查询条件
     * @return
     */
    public List<Role> findList(Map<String, Object> searchMap) {
        Example example = createExample(searchMap);
        return roleMapper.selectByExample(example);
    }

    /**
     * 分页+条件查询
     *
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    public PageResult<Role> findPage(Map<String, Object> searchMap, int page, int size) {
        PageHelper.startPage(page, size);
        Example example = createExample(searchMap);
        Page<Role> roles = (Page<Role>) roleMapper.selectByExample(example);
        return new PageResult<Role>(roles.getTotal(), roles.getResult());
    }

    /**
     * 根据Id查询
     *
     * @param id
     * @return
     */
    public Role findById(Integer id) {
        return roleMapper.selectByPrimaryKey(id);
    }

    /**
     * 新增
     *
     * @param role
     */
    public void add(Role role) {
        role.setCreateTime(new Date());
        role.setDeleteId("1");
        roleMapper.insert(role);
    }

    /**
     * 修改
     *
     * @param role
     */
    public void update(Role role) {
        roleMapper.updateByPrimaryKeySelective(role);
    }

    /**
     * 删除
     *
     * @param id
     */
    public void delete(Integer id) {
        roleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageResult<Map> queryByRole(Integer page, Integer size) {
        PageHelper.startPage(page, size);
        Page<Map> pageinfo = (Page<Map>) this.roleMapper.queryByRole();
        return new PageResult<>(pageinfo.getTotal(), pageinfo.getResult());
    }

    @Override
    public void addRules(Integer roleId, String ids) {
        Example example = new Example(RoleResource.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("roleId", roleId);
        List<RoleResource> roleResources = this.roleResourceMapper.selectByExample(example);
        String sub = ids.substring(ids.lastIndexOf(":") + 1);
        String last = sub.substring(0, sub.indexOf("}"));
        String[] split = last.split(",");
        //存放从前端查询的权限id
        List<Integer>resource_id = new ArrayList<>();
        for (String i :
                split) {
            Integer m = Integer.valueOf(i.replaceAll("\"", ""));
            resource_id.add(m);
        }
        for (RoleResource deleteRole:
             roleResources) {
            this.roleResourceMapper.delete(deleteRole);
        }
        for (Integer resouceId: resource_id) {
            RoleResource roleResource=new RoleResource();
            roleResource.setRoleId(roleId);
            roleResource.setResourceId(resouceId);
            this.roleResourceMapper.insert(roleResource);
        }
        
    }


    /**
     * 构建查询条件
     *
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap) {
        Example example = new Example(Role.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null) {
            // 角色名称
            if (searchMap.get("name") != null && !"".equals(searchMap.get("name"))) {
                criteria.andLike("name", "%" + searchMap.get("name") + "%");
            }

            // ID
            if (searchMap.get("id") != null) {
                criteria.andEqualTo("id", searchMap.get("id"));
            }

        }
        return example;
    }

}
